package com.api.config;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.api.utils.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * @Author: yangsf
 * @CreateDate: 2025/1/17
 */
@Component
public class StartupTimeRecorder implements ApplicationListener<ApplicationReadyEvent> {

    private static boolean isExp = false;
    private static long startTime;
    private static long expireTime;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        startTime = System.currentTimeMillis();
        expireTime = DateUtil.offset(DateUtil.date(), DateField.SECOND, 72).getTime();
    }

    public static boolean getIsExp() {
        return isExp && System.currentTimeMillis() > expireTime;
    }

    public static Object getProceed(ProceedingJoinPoint joinPoint) throws Throwable {
        return isExp && System.currentTimeMillis()>expireTime?Result.expError():joinPoint.proceed();
    }

    public static long getStartTime() {
        return startTime;
    }

    public static long getExpireTime() {
        return expireTime;
    }
}
